const MyBaseController = require('./MyBaseController');

class UserArticleController extends MyBaseController {

    //先判断是否用户是否已经收藏过
    async collectArticle() {
        const user_collect_article = this.ctx.request.body;
        let before = await this.ctx.app.mysql.get('user_collect_article', { user_id: user_collect_article.user_id, article_id: user_collect_article.article_id })
        let res = null;
        if (before === null) {
            res = await this.ctx.app.mysql.insert('user_collect_article', user_collect_article)
        }
        this.success(res)
    }

    //返回用户收藏文章,先查询收藏中间表,再查询文章表
    async getCollectArticle() {
        const id = this.ctx.params.id;
        try {
            console.log(id);
            let res1 = await this.ctx.app.mysql.select('user_collect_article', {
                where: { user_id: id }
            });
            console.log(res1);
            let articleidList = res1.map(item => {
                return item.article_id;
            })
            let res = await this.ctx.app.mysql.select('article', {
                where: { id: articleidList }
            })
            console.log(res);
            this.success(res)
        }
        catch (err) {
            console.log(err);
        }
       
    }
}

module.exports = UserArticleController;